package com.hildeberto.home.client;

import com.conseilimmo.repository.BatimentBsn;
import com.conseilimmo.repository.LocationBsn;
import com.conseilimmo.repository.PersonneBsn;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Pagination;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javax.transaction.Transaction;
import objetsBd.BatimentBd;
import objetsBd.LocataireBd;
import objetsBd.LocationBd;
import objetsBd.PersonneBd;

/**
 *
 * @author Stéphan
 */
public class LocataireController  implements Initializable {
    private List<LocataireBd>locatairesEdition;
    private LocataireBd locataireEnEdition;
    @FXML
    private TextField messageInfo;
 @FXML
    private StackPane pagePanel;
  private ObservableList<Transaction> transactions ;
    @FXML
    private Pagination pagination;
    private IntegerProperty limit;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
       

    }
/**
     * This is the function for setting up the visual representation
     * on the startup of the program
     */
    public void init(){
        resetPage();
        pagination.setCurrentPageIndex(0); 
        changeTableView(0,limit.get());
    }
    
    /**
     * This function resets the pagination pagecount
     */
    public void resetPage(){
        pagination.setPageCount((int)(Math.ceil(transactions.size()*1.0/limit.get()))); 
    }
    
    
    /**
     * This function is responsible for changing the TableView according
     * to the index and limit provided
     * @param index
     * @param limit 
     */
    public void changeTableView(int index,int limit){
        int newIndex = index*limit;
       /* 
        List<Transaction> trans = transactions.subList(Math.min(newIndex,transactions.size()),Math.min(transactions.size(),newIndex+limit));
        table.getItems().clear();
        table.setItems(null);
        ObservableList<Transaction> l = FXCollections.observableArrayList();
        table.setItems(l);        
        for(Transaction t: trans){
           l.add(t); 
        }
        System.out.println("Size:"+l.size());*/
        
    }
    
    
    
    /**
     * This function is called from the FXML when the user enters
     * new limit 
     * @param evt 
     */
    @FXML
    public void changeLimit(ActionEvent evt){
        
       /* TextField txt=(TextField)evt.getSource();
        if(txt != null){
            try{
                int i =Integer.parseInt(txt.getText());
                
                limit.set(i);
                
                resetPage();
                System.out.println(table.getItems().size());
                //pagination.setCurrentPageIndex(Math.min(pagination.getCurrentPageIndex(),table.getItems().size()/limit.get())); 
            }catch(NumberFormatException nfe){
                System.err.println("NFE error");
            }
        }*/
        
    }
    
    /**
     * This function helps to set the cellValueFactory of all column
     */
    private void setValueFactory(){
        /*snCol.setCellFactory(new Callback<TableColumn<Transaction,Integer>,TableCell<Transaction,Integer>>(){


            @Override
            public TableCell<Transaction, Integer> call(TableColumn<Transaction, Integer> param) {
                TableCell cell = new TableCell(){
                    protected void updateItem(Transaction t, boolean bln) {
                        super.updateItem(t, bln); 
                        
                        setText(""+t.snProperty().get());
                    } 
                };
                
                return cell;
            }
            
        });*/
       


    }

    public List<LocataireBd> getLocatairesEdition() {
        return locatairesEdition;
    }

    public void setLocatairesEdition(List<LocataireBd> locatairesEdition) {
        limit=new SimpleIntegerProperty(locatairesEdition.size());
        this.locatairesEdition = locatairesEdition;
    }

    public TextField getMessageInfo() {
        return messageInfo;
    }

    public void setMessageInfo(TextField messageInfo) {
        this.messageInfo = messageInfo;
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public IntegerProperty getLimit() {
        return limit;
    }

    public void setLimit(IntegerProperty limit) {
        this.limit = limit;
    }

    public LocataireBd getLocataireEnEdition() {
        return locataireEnEdition;
    }

    public void setLocataireEnEdition(LocataireBd locataireEnEdition) {
        this.locataireEnEdition = locataireEnEdition;
    }
    
    
    @FXML
    private void btnVoirLocationAction(ActionEvent action) {
        LocationBsn locationBsn = new LocationBsn();
        Long idLocation=null;
        if(locatairesEdition!=null && !locatairesEdition.isEmpty()){
            idLocation=locatairesEdition.get(0).getIdLocation();
        }
        LocationBd location =locationBsn.trouverLocationParId(idLocation);
        
        MainApp.getLocationController().changerLocation(location);
    }
    @FXML
    private void btnBatimentAction(ActionEvent action) {
        LocationBsn locationBsn = new LocationBsn();
        Long idLocation=null;
        if(locatairesEdition!=null && !locatairesEdition.isEmpty()){
            idLocation=locatairesEdition.get(0).getIdLocation();
        }
        LocationBd location =locationBsn.trouverLocationParId(idLocation);
        BatimentBsn batimenBsn =new BatimentBsn();
        BatimentBd bat = batimenBsn.trouverBatimentParId(location.getIdBatiment());
        MainApp.getBatimentController().setBatimentEnCours(bat);
    }
    @FXML
    private void btnPersonneAction(ActionEvent action) {
        LocationBsn locationBsn = new LocationBsn();
        Long idPersonne=null;
        if(locatairesEdition!=null && !locatairesEdition.isEmpty()){
            idPersonne=locatairesEdition.get(0).getIdPersonne();
        }
        PersonneBsn personneBsn =new PersonneBsn();
        PersonneBd personne =personneBsn.trouverPersonneParId(idPersonne);
        
        MainApp.getPersonneController().changerDePersonne(personne);
    }

    
}
